var Secretario = require('../classes/secretario'),
    SecretarioDAO = require('../DAO/secretario.dao');

function SecretarioMapper() {
    var dao = new SecretarioDAO();

    this.secretarioToModel = function(sec, callback) {
        var m = dao.getModel();
        m.nome = sec.getNome();
        m.email = sec.getEmail();
        m.matricula = sec.getMatricula();
        m.username = sec.getUsername();
        m.password = sec.getPassword();
        m.roles = sec.getRoles();
        m.telefone = sec.getTelefone();
        callback(m);
    }

    this.modelToSecretario = function(m, callback) {
        if (m instanceof Array) {
            var out = [];
            m.forEach(function(i) {
                var s = new Secretario();
                s.setNome(i.nome);
                s.setEmail(i.email);
                s.setId(i._id);
                s.setMatricula(i.matricula);
                s.setUsername(i.username);
                s.setPassword(i.password);
                s.setRoles(i.roles);
                s.setTelefone(i.telefone);
                out.push(s);
            });
            callback(out);
        }
        else {
            var s = new Secretario();
            s.setNome(m.nome);
            s.setEmail(m.email);
            s.setMatricula(m.matricula);
            s.setUsername(m.username);
            s.setPassword(m.password);
            s.setRoles(m.roles);
            s.setTelefone(m.telefone);
            s.setId(m._id);
            callback(s);
        }
    }

    this.add = function(secretario) {
        this.secretarioToModel(secretario, function(m) {
            dao.add(m);
        });
    }

    this.get = function(secretario, callback) {
        var that = this;
        dao.get(secretario, function(err, data) {
            if(err) throw err;
            that.modelToSecretario(data, function(s) {
                callback(err, s);
            });
        });
    }

    this.update = function(id, secretario) {
        this.secretarioToModel(secretario, function(m) {
            dao.update(id, m);
        });
    }

    this.remove = function(secretario) {
        this.secretarioToModel(secretario, function(m) {
            dao.remove(m);
        });
    }

    this.getAll = function(callback) {
        var that = this;
        dao.getAll(function(err, all) {
            that.modelToProfessor(all, function(all) {
                callback(err, all);
            });
        });
    }
}

module.exports = SecretarioMapper;
